package com.yjz.study.http.intercept

import com.yjz.common.restful.HttpBaseResponse
import com.yjz.common.restful.Interceptor
import com.yjz.study.account.AccountManager
import com.yjz.study.arouter.ARouterLogic

/**
 *作用 : 状态码拦截器
 *作者 : yjz
 *时间 : 2025/3/13 11:42
 */
class HttpStatusInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Boolean {
        val response = chain.response()
        if (!chain.isRequestPeriod && response != null) {
            when (response.code) {
                // 需要进行登录
                HttpBaseResponse.RC_NEED_LOGIN -> {
                    ARouterLogic.navigation(ARouterLogic.Path.LOGIN_PATH)
                }
                // token错误，跳转到全局错误页
                HttpBaseResponse.RC_AUTH_TOKEN_EXPIRED, (HttpBaseResponse.RC_AUTH_TOKEN_INVALID), (HttpBaseResponse.RC_USER_FORBID) -> {
                    val action = response.errorData?.get("helpUrl")
                    val map = hashMapOf("title" to "非法访问", "desc" to response.msg!!, "action" to action as String)
                    ARouterLogic.navigation(ARouterLogic.Path.DEGRADE_GLOBAL_PATH, map)
                    return true
                }
            }
        }
        return false
    }
}